package com.ltg.topactivity;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class PopView extends LinearLayout {
  private WindowManager.LayoutParams mWindowParams;
  private WindowManager mWindowManager;
  private ImageButton mHandle;
  private View mContent;
  private int screenWidth, screenHeight;

  public PopView(Context context, View content) {
    super(context);
    mWindowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    mContent = content;

    getScreenWH();
    initWindowParam();

    resetHandle();
  }

  private void initWindowParam() {
    mWindowParams = new WindowManager.LayoutParams();
    mWindowParams.type = WindowManager.LayoutParams.TYPE_PHONE;
    mWindowParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    mWindowParams.width = screenWidth / 2;
    mWindowParams.height = screenHeight / 2;
    mWindowParams.gravity = Gravity.RIGHT | Gravity.TOP;
    mWindowParams.format = PixelFormat.RGBA_8888;
    mWindowParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    mWindowParams.x = 0;
    mWindowParams.y = 0;
  }

  private void resetHandle() {
    addHandleIfNeeded();
    removeView(mContent);
  }

  private void addHandleIfNeeded() {
    if (null == mHandle) {
      mHandle = new ImageButton(getContext());
      mHandle.setImageResource(android.R.drawable.ic_menu_search);
      mHandle.setOnTouchListener(new HandleTouchListener());
      addView(mHandle);
    }
  }

  @Override
  protected void onConfigurationChanged(Configuration newConfig) {
    getScreenWH();
  }

  private void getScreenWH() {
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    screenWidth = displayMetrics.widthPixels;
    screenHeight = displayMetrics.heightPixels;
  }

  private void close() {
    if (mContent.isShown()) {
      removeView(mContent);
    }
  }

  private void open() {
    if (!mContent.isShown()) {
      addView(mContent);
    }
  }

  private void toggle() {
    if (mContent.isShown()) {
      close();
    } else {
      open();
    }
  }

  public void show() {
    if (!isShown()) {
      mWindowManager.addView(this, mWindowParams);
    }
    open();
  }

  public void dismiss() {
    if (isShown()) {
      mWindowManager.removeView(this);
    }
  }

  public void update(float x, float y) {
    mWindowParams.x += x;
    mWindowParams.y += y;
    mWindowManager.updateViewLayout(this, mWindowParams);
  }

  private class HandleTouchListener implements View.OnTouchListener {

    private static final int TOUCH_SLOOP = 10;
    private float lastX, lastY;
    private boolean moved;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
      float x = event.getRawX(), y = event.getRawY();
      int action = event.getAction();
      if (action == MotionEvent.ACTION_DOWN) {
        lastX = x;
        lastY = y;
        moved = false;
      } else if (action == MotionEvent.ACTION_MOVE) {
        if (Math.abs(x - lastX) > TOUCH_SLOOP || Math.abs(y - lastY) > TOUCH_SLOOP) {
          if (!moved) {
            moved = true;
            resetHandle();
          }
          update(x - lastX, y - lastY);
          lastX = x;
          lastY = y;
        }
      } else if (action == MotionEvent.ACTION_UP) {
        if (moved) {
          resetHandle();
        } else {
          toggle();
        }
      }
      return true;
    }

  }

}
